package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_CROSS_PRE_OPEN_BANQUET, desc = "开启跨服宴会")
public class PreOpenBanquetCmd extends CrossCommand {


    private long userId;
    private int type;
    private String invitationId;
    private String invitationInfo;
    private String serverListStr;
    private UserBaseInfo userBaseInfo;
    private String giftTypeAccept;

    private String banquetId;

    private boolean autoOpen;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossBanquetProto.PreOpenCrossBanquetReq reqMsg = CrossBanquetProto.PreOpenCrossBanquetReq.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        type = reqMsg.getType();
        serverListStr = reqMsg.getServerListStr();
        invitationId = reqMsg.getInvitationId();
        invitationInfo = reqMsg.getInvitationInfo();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
        giftTypeAccept="";
        if (reqMsg.hasGiftTypeAccept()) {
            giftTypeAccept = reqMsg.getGiftTypeAccept();
        }
        if (reqMsg.hasAutoOpen()){
            autoOpen = reqMsg.getAutoOpen();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int res = CrossBanquetMgr.checkOpenBanquet(userId,type,serverListStr);
        CrossBanquetProto.PreOpenCrossBanquetResp.Builder respMsg = CrossBanquetProto.PreOpenCrossBanquetResp.newBuilder();
        respMsg.setAutoOpen(autoOpen);
        if(res == 0){
            banquetId = CrossBanquetMgr.preOpenBanquet((int)serverId,userId,userBaseInfo,type,invitationId,invitationInfo,serverListStr,giftTypeAccept, autoOpen);
            respMsg.setType(type);
            respMsg.setInvitationId(invitationId);
            respMsg.setBanquetId(banquetId);
            respMsg.setServerListStr(serverListStr);
            respMsg.setGiftTypeAccept(giftTypeAccept);
        }
        respMsg.setRet(res);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_BANQUET_CROSS_PRE_OPEN_RESP, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
